package com.custom.base.util

import android.util.SparseArray
import android.view.View

class ViewHolder {
    companion object {

        /**
         * 用public static <T extends View> T get(int id, View convertView)方法替代此方法
         *
         * @param convertView
         * @param id
         * @return
        </T> */
        @Deprecated("")
        operator fun <T : View> get(convertView: View, id: Int): T {
            var viewHolder: SparseArray<View>? = convertView.tag as SparseArray<View>
            if (viewHolder == null) {
                viewHolder = SparseArray()
                convertView.tag = viewHolder
            }
            var childView: View? = viewHolder.get(id)
            if (childView == null) {
                childView = convertView.findViewById(id)
                viewHolder.put(id, childView)
            }
            return childView as T
        }

        operator fun <T : View> get(id: Int, convertView: View): T {
            var viewHolder: SparseArray<View>? = convertView.tag as SparseArray<View>
            if (viewHolder == null) {
                viewHolder = SparseArray()
                convertView.tag = viewHolder
            }
            var childView: View? = viewHolder.get(id)
            if (childView == null) {
                childView = convertView.findViewById(id)
                viewHolder.put(id, childView)
            }
            return childView as T
        }
    }
}
